import { color, find, Prefab, ScrollView, Sprite, SpriteFrame } from 'cc';
import { view } from 'cc';
import { instantiate } from 'cc';
import { tween } from 'cc';
import { UITransform } from 'cc';
import { _decorator, Component, Label, Node } from 'cc';
import { App } from '../../../App';
import AbstractComponent from '../../../base/AbstractComponent';
import { EventConst } from '../../../common/Common';
import { PD } from '../../../player/PlayerData';
import { showLuckyBallRule, showTipBox } from '../../../tools/PopuUp';
import UtilTool from '../../../tools/UtilTool';
import { BLACK_BALL } from '../GameManage';
import { GameModel } from '../model/GameModel';
import { GameLayer3 } from './GameLayer3';
import { GameModel3 } from './GameModel3';
import { GameMulitUser } from './GameMulitUser';
const { ccclass, property } = _decorator;

// 所有用户的查看详情界面
@ccclass('GameMulitUserDetail')
export class GameMulitUserDetail extends Component {
    @property(Node)
    contentNode : Node = null

    @property(Prefab)
    ball_item : Prefab = null

    @property(ScrollView)
    scroll : ScrollView = null

    @property(Node)
    cellNode : Node = null

    start() {
        this.node.getComponent(UITransform).setContentSize(view.getVisibleSize())
        UtilTool.nodeActive(this.contentNode, 5)

        let gameView = App.Model.gameView as GameModel3
        gameView.reqUsersBallListMsg(this.setData.bind(this))
        // this.setData()
    }

    // 设置球数据
    setData(data) {
        this.cellNode.active = false
        let admitDefeatPlayerList = data.admitDefeatPlayerList // 认输玩家列表
        let disconnectionPlayerList = data.disconnectionPlayerList // 掉线玩家列表
        let notEnterBall = data.notEnterBall // 玩家未进的球的列表（map，key为玩家账号，value为目标球列表）

        let addressArr = [] // 玩家钱包地址列表
        let ballsArr = []   // 玩家剩余球列表
        for (const key in notEnterBall) {
            if (Object.prototype.hasOwnProperty.call(notEnterBall, key)) {
                let value = notEnterBall[key];
                addressArr.push(key)
                ballsArr.push(value)
            }
        }

        let count = Object.keys(notEnterBall).length
        let oneH = 140
        let height = oneH * count
        if (height<this.scroll.content.getComponent(UITransform).height) {
            height = this.scroll.content.getComponent(UITransform).height
        }
        this.scroll.content.getComponent(UITransform).setContentSize(this.scroll.content.getComponent(UITransform).contentSize.width, height)

        let bigDict = App.Scene.getRes("images/ball2D")
        for (let i = 0; i < count; i++) {
            // 创建节点
            let cell = instantiate(this.cellNode)
            cell.active = true
            cell.parent = this.scroll.content
            cell.setPosition(0,height-oneH*i)

            let address = find("address",cell)
            let slot = find("slot",cell)

            address.getComponent(Label).string = UtilTool.formatNickOrAddress({"nickname":App.Model.gameManage.gameStartData.nicknameMap[addressArr[i]], "address":addressArr[i]})

            // 掉线处理
            if (disconnectionPlayerList.indexOf(addressArr[i]) != -1) {
                address.getComponent(Label).color = color(238,70,70,255)
            }
            else{
                address.getComponent(Label).color = color(255,255,255,255)
            }

            // 显示球
            let allBallList = ballsArr[i]
            for (let index = 0; index < allBallList.length; index++) {
                let num = allBallList[index];
                let node = instantiate(this.ball_item)
    
                // if(num < BLACK_BALL) {
                //     let spDict = App.Scene.getRes("images/ball2D_2")
                //     node.getComponent(Sprite).spriteFrame = spDict["ball2d_" + item]
                // } else {
                    node.getComponent(Sprite).spriteFrame = bigDict["ball2d_" + num]
                // }
    
                node.parent = slot
                node.getComponent(UITransform).setContentSize(60,60)
                let posx = UtilTool.getSortPosX(allBallList.length, 60, index)
                node.setPosition(posx, 0)

                // 认输标签
                let xSpr = find("xSpr",node)
                if (admitDefeatPlayerList.indexOf(addressArr[i]) != -1) {
                    xSpr.active = true
                }
                else{
                    xSpr.active = false
                }
            }
        }

        this.scroll.scrollToTop()
    }

    /** 
     * 确定
      */
    clickSure() {
        App.Audio.playOneShot("click")
        this.node.destroy()
    }

    onDestroy() {
        
    }

}

